﻿using NDK.Motion.MachineResources;
using System;
using System.Linq;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Frm_VelProfile : Form
    {
        private static log4net.ILog traceLogger = log4net.LogManager.GetLogger("Frm_VelProfile");

        public Station CurStation = new Station();
        public Position CurPosition = new Position();
        public DataGridViewRow CurPositionTable;

        public Frm_VelProfile()
        {
            InitializeComponent();
        }

        private void Frm_VelProfile_Load(object sender, EventArgs e)
        {
            foreach (var axis in CurPosition.AxisList)
            {
                string strPos = CurPositionTable.Cells[axis.Name].Value.ToString();
                VelocityProfile velProfile = CurPosition.VelocityProfiles.SingleOrDefault(v => v.AxisName == axis.Name);
                double pos = 0.0;
                double.TryParse(strPos, out pos);

                velProfile.Pos = pos;

            }
            dgv_VelProfile.DataSource = CurPosition.VelocityProfiles;
        }

        private void dgv_VelProfile_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;

            var axis = CurPosition.AxisList.Find(a => a.Name == dgv_VelProfile.Rows[e.RowIndex].Cells["vel_AxisName"].Value.ToString());


            string strMoveVel = dgv_VelProfile.Rows[e.RowIndex].Cells["vel_MoveVel"].Value.ToString();
            double maxVel = axis.MaxVel;
            double moveVel = 0.0;
            bool isMoveVelDataOk = double.TryParse(strMoveVel, out moveVel);

            if (isMoveVelDataOk && maxVel < moveVel)
            {
                dgv_VelProfile.Rows[e.RowIndex].Cells["vel_MoveVel"].ErrorText = "输入运行速度大于设置的最大速度！";
            }
            else
            {
                dgv_VelProfile.Rows[e.RowIndex].Cells["vel_MoveVel"].ErrorText = "";
            }

            if (!isMoveVelDataOk)
            {
                dgv_VelProfile.Rows[e.RowIndex].Cells["vel_MoveVel"].ErrorText = "输入类型错误！";
            }

        }

        private void dgv_VelProfile_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.ThrowException = false;
        }
    }
}
